package com.declum.peyar.client;

import java.io.DataInput;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class Peyar {

	static String address;
	/**
	 * Constants for describing the Commands
	 */
	protected static final String BIND = "bind";
	protected static final String LOOKUP = "lookup";
	static int port;
	protected static final String UNBIND = "unbind";

	public static void bind(String name, String identifier) throws IOException,
			HeaderNotProper {
		Socket socket = Peyar.connect();
		String line;
		String header = "";
		PrintWriter writter = Peyar.getWritter(socket);
		writter.println("name: " + name);
		writter.println("address: " + identifier);
		writter.println("command: " + Peyar.BIND);
		writter.flush();

		DataInput reader = Peyar.getReader(socket);

		do {
			line = reader.readLine();
			if (line == null) {
				header += "\r\n";
				break;
			}
			header += line + "\r\n";

		} while (true);
		socket.close();

		Peyar.checkResult(header);
	}

	static void checkResult(String responseHeader) throws HeaderNotProper {
		if (responseHeader.endsWith("\r\n\r\n")) {
			// TODO More checks here
		} else {
			throw new HeaderNotProper("Header is not complete");
		}
	}

	public static String[] lookup(String name) throws IOException,
			HeaderNotProper {
		Socket socket = Peyar.connect();
		String line;
		String header = "";
		PrintWriter writter = Peyar.getWritter(socket);
		writter.println("name: " + name);
		writter.println("command: " + Peyar.LOOKUP);
		writter.flush();

		DataInput reader = Peyar.getReader(socket);

		do {
			line = reader.readLine();
			if (line == null) {
				header += "\r\n";
				break;
			}
			header += line + "\r\n";

		} while (true);
		socket.close();
		Peyar.checkResult(header);

		Headers headers = new Headers();
		headers.parseResponse(header);
		String addresses = headers.getHeader("address");
		if ((addresses == null)) {
			return new String[] {};
		}

		addresses = addresses.trim();
		if (addresses.trim().equals("")) {
			return new String[] {};
		}

		return addresses.split(" ");
	}

	public static boolean unbind(String name, String identifier)
			throws IOException, HeaderNotProper {
		Socket socket = Peyar.connect();
		String line;
		String header = "";
		PrintWriter writter = Peyar.getWritter(socket);
		writter.println("name: " + name);
		writter.println("address: " + identifier);
		writter.println("command: " + Peyar.UNBIND);
		writter.flush();

		DataInput reader = Peyar.getReader(socket);

		do {
			line = reader.readLine();
			if (line == null) {
				header += "\r\n";
				break;
			}
			header += line + "\r\n";

		} while (true);
		socket.close();
		Peyar.checkResult(header);
		return true;
	}

	public static void configure(String address, int port) {
		Peyar.address = address;
		Peyar.port = port;
	}

	private static Socket connect() throws UnknownHostException, IOException {
		Socket socket = new Socket(Peyar.address, Peyar.port);
		return socket;
	}

	static DataInput getReader(Socket socket) throws IOException {
		DataInputStream in = new DataInputStream(socket.getInputStream());
		return in;
	}

	private static PrintWriter getWritter(Socket socket) throws IOException {
		PrintWriter writter = new PrintWriter(socket.getOutputStream());
		return writter;
	}

	public static void main(String[] args) throws IOException, HeaderNotProper {
		Peyar.configure("localhost", 9123);
		Peyar.bind("peyar", "secondaray-server-two");
		Peyar.lookup("peyar");
		Peyar.unbind("peyar", "secondaray-server-two");
		Peyar.lookup("peyar");
	}
}
